"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorChooseinfoCtrl
 * @description
 * # AdministratorChooseinfoCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "AdministratorChoosewz",
    function ($scope, Util, openApi, localStorageService, PagingService, $location) {
      var url, path;
      path = $location.path();
      if (path.indexOf("jwz") > -1) {
        url = openApi.jwzlist;
      } else if (path.indexOf("hwz") > -1) {
        url = openApi.hwzlist;
      } else {
        url = openApi.wzlist;
      }
      var param = {};
      param = Util.getDataforsearch();
      param.callback = function (data) {
        $scope.selectLists.forEach(function (item, index) {
          data.forEach(function (subitem, index) {
            // 设置选中状态
            if (item.pkId == subitem.pkId) {
              subitem.selected = true;
            }
          });
        });
      };
      // -----------------底部款项类型选框添加end-----------------------
      $scope.choosePeople = function (item) {
        if ((path.indexOf("wzk") > -1)) {
          $scope.selectLists=[item]
          $scope.submit();
          return
        }
        $scope.searchInput = "";
        item.selected = !item.selected;
        // 同步选择的数据
        if (item.selected) {
          $scope.selectLists.push(item);
        } else {
          $scope.selectLists = $scope.selectLists.filter(
            function (subitem, index) {
              return item.pkId != subitem.pkId;
            }
          );
        }
      };
      // 搜索
      $scope.getData = function () {
        // 参训参数不一样就两个都传了
        param.mname = $scope.searchInput || "";
        param.name = $scope.searchInput || "";
        $scope.taskList = new PagingService(url, param);
      };

      $scope.search = function () {
        $scope.getData();
      };
      // 获取数据返回
      $scope.submit = function () {
        if ($scope.selectLists.length < 1) {
          Util.Toast("请选择");
          return;
        }
        localStorageService.set("selectLists", $scope.selectLists);
        Util.goBack();
      };
      // 页面初始化请求数据
      var init = function () {
        $scope.getData();
        $scope.selectLists = Util.getSelectLists() || [];
      };
      init();
    }
  );
